#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>



bool MyCompare(int a, int b) {

    int aTmp = a;
    int bTmp = b;

    int aSum = 0;
    int bSum = 0;

    while(a) {
        aSum += a % 10;
        a /= 10;
    }

    while(b) {
        bSum += b % 10;
        b /= 10;
    }

    if (aSum == bSum) {
        return aTmp < bTmp;
    }
    return aSum < bSum;
}


/**
 * 
 * 时间复杂度 O(nlogn * logM) (M是最大的数字)
 * 空间复杂度 O(1)
 * 
 * 
 * 整数排序进阶
 * @return 
 */
int main() {

    int arr[110]{};

    int n;

    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    std::sort(arr, arr + n, MyCompare);

    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }


    return 0;
}